package com.example.webbasic.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.example.webbasic.AppUtils;

/**
 * 第二个拦截器
 * @author ben
 * @date 2021-11-07 15:29:14 CST
 */
public class MyInterceptor2 implements HandlerInterceptor {

	// 请求处理前调用（Controller方法调用之前）
	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse response, Object handler)
			throws Exception {
		AppUtils.pt("MyInterceptor2 url=" + req.getRequestURL() + ", 处理前");
//		return HandlerInterceptor.super.preHandle(req, response, handler);
		
		String stop = req.getParameter("stop");
		if (stop == null || !stop.equals("true")) {
			// 返回true才会向下继续执行
			return true;
		} else {
			AppUtils.pt("MyInterceptor2 url=" + req.getRequestURL() + ", 处理前, 停止处理！");
			return false;
		}
	}

	// 请求处理之后调用，但是在视图被渲染之前（Controller方法调用之后）
	@Override
	public void postHandle(HttpServletRequest req, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		HandlerInterceptor.super.postHandle(req, response, handler, modelAndView);
		AppUtils.pt("MyInterceptor2 url=" + req.getRequestURL() + ", 处理后");
	}

	// 在整个请求结束之后被调用
	// DispatcherServlet渲染了对应的视图之后执行
	// 主要用于进行 资源清理工作
	@Override
	public void afterCompletion(HttpServletRequest req, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		HandlerInterceptor.super.afterCompletion(req, response, handler, ex);
		AppUtils.pt("MyInterceptor2 url=" + req.getRequestURL() + ", 处理完成后");
	}

}
